import {
    getPlayerData,
    storagePlayerData,
    ensureGroupAndPlayerExist,
    getConfig
} from "../function/function.js";

export class Bank extends plugin {
    constructor() {
        super({
            name: '[Sm]银行',
            dsc: '银行功能',
            event: 'message',
            priority: 1,
            rule: [
                { reg: /^[#\/]存款\s*(\d+)$/, fnc: 'deposit' },
                { reg: /^[#\/]取款\s*(\d+)$/, fnc: 'withdraw' },
                { reg: /^[#\/]升级信用$/, fnc: 'upgradeCredit' },
                { reg: /^[#\/]银行信息$/, fnc: 'bankInfo' }
            ]
        })
    }

    // 初始化银行数据
    async initBankData(userData) {
        const config = getConfig();
        if (!userData.bank) {
            userData.bank = {
                balance: 0,  // 存款余额
                level: config.bank.initialLevel,  // 信用等级
                limit: config.bank.initialLimit,  // 存储上限
                upgradePrice: config.bank.initialUpgradePrice  // 升级价格
            };
        }
        return userData;
    }

    // 存款
    async deposit(e) {
        if (!e.isGroup) return e.reply(['该功能只能在群内使用']);
        const ID = [e.user_id, e.group_id];
        
        ensureGroupAndPlayerExist(ID[1], ID[0]);
        let userData = await getPlayerData(ID[1], ID[0]);
        userData = await this.initBankData(userData);

        const amount = parseInt(e.msg.match(/\d+/)[0]);
        if (isNaN(amount) || amount <= 0) return e.reply(['请输入正确的存款金额']);
        
        if (amount > userData.currency) return e.reply(['余额不足']);
        if (userData.bank.balance + amount > userData.bank.limit) {
            return e.reply([
                `存款失败！当前存储上限为${userData.bank.limit}金币\n`,
                `当前存款：${userData.bank.balance}金币\n`,
                `可存入：${userData.bank.limit - userData.bank.balance}金币\n`,
                '可通过升级信用等级提升存储上限'
            ]);
        }

        userData.currency -= amount;
        userData.bank.balance += amount;
        storagePlayerData(ID[1], ID[0], userData);

        return e.reply([
            `存款成功！存入${amount}金币\n`,
            `当前存款：${userData.bank.balance}金币\n`,
            `当前余额：${userData.currency}金币`
        ]);
    }

    // 取款
    async withdraw(e) {
        if (!e.isGroup) return e.reply(['该功能只能在群内使用']);
        const ID = [e.user_id, e.group_id];
        
        ensureGroupAndPlayerExist(ID[1], ID[0]);
        let userData = await getPlayerData(ID[1], ID[0]);
        userData = await this.initBankData(userData);

        const amount = parseInt(e.msg.match(/\d+/)[0]);
        if (isNaN(amount) || amount <= 0) return e.reply(['请输入正确的取款金额']);
        
        if (amount > userData.bank.balance) return e.reply(['存款余额不足']);

        userData.currency += amount;
        userData.bank.balance -= amount;
        storagePlayerData(ID[1], ID[0], userData);

        return e.reply([
            `取款成功！取出${amount}金币\n`,
            `当前存款：${userData.bank.balance}金币\n`,
            `当前余额：${userData.currency}金币`
        ]);
    }

    // 升级信用
    async upgradeCredit(e) {
        if (!e.isGroup) return e.reply(['该功能只能在群内使用']);
        const ID = [e.user_id, e.group_id];
        
        ensureGroupAndPlayerExist(ID[1], ID[0]);
        let userData = await getPlayerData(ID[1], ID[0]);
        userData = await this.initBankData(userData);

        const config = getConfig();
        
        if (userData.currency < userData.bank.upgradePrice) {
            return e.reply([
                `升级失败！升级需要${userData.bank.upgradePrice}金币\n`,
                `当前余额：${userData.currency}金币`
            ]);
        }

        // 扣除升级费用
        userData.currency -= userData.bank.upgradePrice;
        
        // 提升信用等级和存储上限
        userData.bank.level += 1;
        userData.bank.limit = Math.floor(userData.bank.limit * config.bank.limitIncreaseMulti);
        
        // 计算下一级升级价格
        userData.bank.upgradePrice = Math.floor(userData.bank.upgradePrice * config.bank.upgradePriceMulti);
        
        storagePlayerData(ID[1], ID[0], userData);

        return e.reply([
            `升级成功！当前信用等级：${userData.bank.level}\n`,
            `当前存储上限：${userData.bank.limit}金币\n`,
            `下次升级费用：${userData.bank.upgradePrice}金币\n`,
            `当前余额：${userData.currency}金币`
        ]);
    }

    // 查看银行信息
    async bankInfo(e) {
        if (!e.isGroup) return e.reply(['该功能只能在群内使用']);
        const ID = [e.user_id, e.group_id];
        
        ensureGroupAndPlayerExist(ID[1], ID[0]);
        let userData = await getPlayerData(ID[1], ID[0]);
        userData = await this.initBankData(userData);

        return e.reply([
            `=====银行信息=====\n`,
            `信用等级：${userData.bank.level}\n`,
            `当前存款：${userData.bank.balance}金币\n`,
            `存储上限：${userData.bank.limit}金币\n`,
            `升级费用：${userData.bank.upgradePrice}金币\n`,
            `当前余额：${userData.currency}金币`
        ]);
    }
} 